package org.dam.controller;

import org.dam.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author dam
 * @create 2024/1/8 11:38
 */
@Controller
public class AjaxController {

    @RequestMapping("/a1")
    public void ajax1(String name, HttpServletResponse response) throws IOException {
        if ("admin".equals(name)) {
            response.getWriter().print("true");
        } else {
            response.getWriter().print("false");
        }
    }

    @GetMapping("/a2")
    @ResponseBody
    public List<User> ajax2() {
        List<User> list = new ArrayList<User>();
        list.add(new User("张三1号", 3, "男"));
        list.add(new User("张三2号", 31, "男"));
        list.add(new User("李四", 13, "女"));
        //由于@RestController注解，将list转成json格式返回
        return list;
    }

    @RequestMapping("/login")
    public String sayHello(Model model){
        //web-inf/jsp/helloMvc.jsp
        return "login";
    }

    @RequestMapping("/a3")
    @ResponseBody
    public String ajax3(String name, String pwd) {
        String msg = "";
        //模拟数据库中存在数据
        if (name != null) {
            if ("admin".equals(name)) {
                msg = "OK";
            } else {
                msg = "用户名输入错误";
            }
        }
        if (pwd != null) {
            if ("123456".equals(pwd)) {
                msg = "OK";
            } else {
                msg = "密码输入有误";
            }
        }
        //由于@ResponseBody注解，将msg转成json格式返回
        return msg;
    }

    @RequestMapping("/baidu")
    public String baidu(Model model){
        //web-inf/jsp/helloMvc.jsp
        return "baidu";
    }

}